<?php
class Arm_Db_Table_Row_Abstract extends Zend_Db_Table_Row_Abstract{

    public  $_infoTable       = array();
    public  $_combinations    = array();
    public  $_class_table     = Null;

    /**
     *  Sobrescrito método do parent para funcionar relacionamentos
     *  e definições
     */
    public function __construct(array $config = array())
    {
        if (isset($config['table']) && $config['table'] instanceof Zend_Db_Table_Abstract) {
            $this->_table = $config['table'];
            $this->_tableClass = get_class($this->_table);
        } elseif ($this->_tableClass !== null) {
            $this->_table = $this->_getTableFromString($this->_tableClass);
        }

        if (isset($config['data'])) {
            if (!is_array($config['data'])) {
                require_once 'Zend/Db/Table/Row/Exception.php';
                throw new Zend_Db_Table_Row_Exception('Data must be an array');
            }
            $this->_data = $config['data'];
        }
        if (isset($config['stored']) && $config['stored'] === true) {
            $this->_cleanData = $this->_data;
        }

        if (isset($config['readOnly']) && $config['readOnly'] === true) {
            $this->setReadOnly(true);
        }

        // Retrieve primary keys from table schema
        if (($table = $this->_getTable())) {
            $info = $table->info();
            $this->_primary = (array) $info['primary'];
        }
        $this->_class_table = $this->getTable();
        $this->_infoTable   = $this->_class_table->info();

        $this->init();
    }

    public function __toString(){
        ### Se o objeto for nulo, retorna nulo
        if(is_null($this->id))
            return "";

        if($this->_class_table->i18n){
            $i18n           = $this->getI18n();

            if($i18n != false)
                return $i18n->text;
        }
       
        if(isset($this->name))
            return $this->name;

        if(isset($this->id))
            return (string)$this->id;

        return 'Row of ' . get_class($this->_class_table);
    }

    public function delete(){
        if($this->getTable()->hasColumn('deleted')){
            $this   ->deleted   = true;
            $this   ->save();
        }
        else{
            parent::delete();
        }
    }

    public function __get($columnName){
        if(isset($this->_infoTable['referenceMap']) and 
        array_key_exists($columnName,$this->_infoTable['referenceMap'])){

            return $this->get_parent($columnName);
        }elseif(isset($this->_infoTable['dependentTables']) and            
        array_key_exists($columnName,$this->_infoTable['dependentTables'])){

            return $this->get_dependent($columnName);
        }else{
            return $this->getColumn($columnName);
        }
    }

    public function __set($columnName, $value){
   
        if($value instanceof Arm_Db_Table_Row_Abstract){
        
            $value = $valus->key();
        }

        parent::__set($columnName, $value);
    }

    public function getColumn($columnName){
        
        $value = parent::__get($columnName);
    
        if(isset($this->_infoTable['metadata'])){
            if(isset($this->_infoTable['metadata'][$columnName])){

                $property = $this->_infoTable['metadata'][$columnName];
                if($property['DATA_TYPE'] == 'timestamp' and !is_null($value))
                    $value = new Arm_Date($value,Zend_Date::ISO_8601);
            }
        }

        return $value; 
    }
    
    public function preSave(){}
    public function posSave(){}

    public function save(){
   
        $this->preSave();
        $id = parent::save();
        $this->posSave();
        return $id;
    }

    public function key(){
        $key = $this->_infoTable['primary'];
        if(count($key) == 1)
            $key = $key[1];

        return $this->$key;
    }
    
    public function get_parent($name){

        $model            = new $this->_infoTable['referenceMap'][$name]['refTableClass'];
        $reference_column = $this->_infoTable['referenceMap'][$name]['columns'];
        if(is_array($reference_column)) return $this->findParentRow($model,$name);
          
        $reference_id     = $this->_data[$reference_column];  
        
        $records          = Arm_Registry_Row::getInstance();

        if($records->isRegistred($model,$reference_id)){
            return $records->get();
        }else{
            $parent = $this->findParentRow($model,$name);
            if(!is_null($parent)) $records->set($parent);
            return $parent;
        }
    }

    public function get_dependent($name){

        $model  = new $this->_infoTable['dependentTables'][$name];
        $origin = null;

        if(isset($this->_infoTable['dependentTables']['_merge'])){
            foreach($this->_infoTable['dependentTables']['_merge'] as $table => $ref){
                if(in_array($name,$ref)){
                    $origin = $table;
                }
            }
        }

        $model->addWhereByParent($this,$origin);
        return $model;
    }
}
